//
//  BanBu_FullScreenGIF.m
//  BanBu
//
//  Created by Wang Xj on 13-9-6.
//
//

#import "BanBu_FullScreenGIF.h"
#import "AppCommunicationManager.h"
#import "BanBu_ChatViewController.h"
#import "AppDataManager.h"

@implementation BanBu_FullScreenGIF

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.smallImage = [[[UIImageView alloc]initWithFrame: CGRectMake(0, 0, frame.size.width,frame.size.height)] autorelease];
        self.smallImage.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnAction:)];
        [self.smallImage addGestureRecognizer:tap];
        
        [tap release];
        
        [self addSubview:self.smallImage];
        
        _status = 3;
        
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"file-loading.png"]];
        
        self.mediaView = [[[SCGIFImageView alloc] init] autorelease];
    }
    return self;
}

-(void)setimageFrame:(CGRect)rect
{
    self.smallImage.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
}

//设置缩略图文件路径
-(void)setMediaGIFPath:(NSString *)fileUrl
{
    NSString * filePath = [AppComManager pathForMsgMedia:fileUrl];
    if (filePath)
    {
        UIImage * image = [UIImage imageWithContentsOfFile:filePath];
        self.smallImage.image = image;
    }
}

//设置图片路径
-(void)setImagePath:(NSString *)imagePath
{
    self.imageFilePath = [AppComManager pathForMsgMedia:imagePath];
    
    if (!self.atLift)
    {
        return;
    }
    
    if ([FileManager fileExistsAtPath:self.imageFilePath])
    {
        if (self.statusNow !=MediaStatusReaded && self.atLift)
        {
            [MyAppDataManager setMediaStatusReaded:self.chatid userID:MyAppDataManager.chatuid];
            [MyAppDataManager readTalkList:MyAppDataManager.chatuid WithNumber:MyAppDataManager.dialogs.count];
        }
        
        if (self.statusNow == MediaStatusReaded || self.statusNow == MediaStatusDownload)
        {
            return;
        }
        
        if (!self.chatController.isStayInChatPage)
        {
            return;
        }
        
        [self.chatController tapped:nil];
        [self btnAction:nil];
    }
}

-(void)setStatus:(MediaStatus)status
{
    if(_status != status)
    {
        _status = status;
        
        if(_status != MediaStatusNormal && _status != MediaStatusReaded)
        {
            if (![FileManager fileExistsAtPath:self.imageFilePath])
            {
                self.smallImage.alpha = .5;
                self.smallImage.userInteractionEnabled = NO;
            }
        }
        else
        {
            if ([FileManager fileExistsAtPath:self.imageFilePath])
            {
                self.smallImage.alpha = 1;
                self.smallImage.userInteractionEnabled = YES;
            }
        }
    }
    [self layoutSubviews];
}

//播放图片
-(void)btnAction:(UITapGestureRecognizer *)tap
{
    if ((UIView *)[self.chatController.navigationController.view viewWithTag:9876500] !=nil)
    {
        return;
    }
    
    if (self.statusNow != MediaStatusReaded)
    {
        [MyAppDataManager setMediaStatusReaded:self.chatid userID:MyAppDataManager.chatuid];
    }
    
    self.statusNow = MediaStatusReaded;
    
    self.bgView = [[[UIView alloc]initWithFrame:__MainScreenFrame] autorelease];
    self.bgView.backgroundColor = [UIColor clearColor];
    self.bgView.tag = 9876500;
    
    self.bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer * shrinkMediaViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shrinkMediaView:)];
    [self.bgView addGestureRecognizer:shrinkMediaViewTap];
    
    [self.chatController.navigationController.view addSubview:self.bgView];
    
    self.mediaView.gifFile = self.imageFilePath;
    self.mediaView.tag = 9876501;
    self.mediaView.frame = CGRectMake(0, 0, 0, 0);
    self.mediaView.center = self.chatController.navigationController.view.center;
    [self.chatController.navigationController.view addSubview:self.mediaView];
    
    CGSize size = self.mediaView.image.size;
    if (size.width>320)
    {
        size.height = size.height*320/size.width;
        size.width = 320;
    }
    
    [UIView animateWithDuration:0.4 animations:^{
        self.mediaView.bounds = CGRectMake(0, 0, size.width, size.height);
    }];
}

//消失
-(void)shrinkMediaView:(UITapGestureRecognizer *)tap
{
        [UIView animateWithDuration:0.5
                     animations:^{
                         self.mediaView.bounds = CGRectMake(0, 0, 0, 0);
                         self.mediaView.center = self.chatController.navigationController.view.center;
                         
                     }
                     completion:^(BOOL finished) {
                         [self.mediaView removeFromSuperview];
                         [self.bgView removeFromSuperview];
                     }];
}

-(void)dealloc
{    
    [_chatid release],_chatid = nil;
    [_bgView release],_bgView = nil;
    [_mediaView release],_mediaView = nil;
    [_imageFilePath release],_imageFilePath = nil;

    [super dealloc];
}

@end
